//
//  LBIMManager.m
//  app
//
//  Created by 刘文扬 on 16/8/22.
//  Copyright © 2016年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBIMManager.h"
#import "LBIMHistoryMessageListModel.h"
#import <LBService.h>
#import "LBRongYunKitHead.h"
#import <MJExtension.h>

@implementation LBIMManager


+(void)getUserInfoWithIMUserId:(NSString *)imUserId success:(void (^)(LBIMUserInfo *userInfo))success fail:(void (^)(NSString *message))fail
{
    
    
    [LBService get:LBURL_IM(URL_IM_GetUserInfo) params:@{@"imUserId":LBStringNotNil(imUserId)} completion:^(LBResponse *response) {
        
        if (response.succeed) {
            
            LBIMUserInfo *userInfo = [LBIMUserInfo mj_objectWithKeyValues:response.data];
            
            success(userInfo);
            
        }else{
            
            fail(response.message);
        }
        
        
        
    }];
}

+(void)getIMHistoryMessageByContent:(NSString *)content
                   conversationType:(NSInteger)conversationType
                          pageIndex:(NSInteger)pageIndex
                           pageSize:(NSInteger)pageSize
                           toUserId:(NSString *)toUserId
                            success:(LBRCRequestSuccessDataBlock)success
                               fail:(LBRCRequestFailBlock)fail
{
    NSDictionary *dic = @{@"content":LBStringNotNil(content),
                          @"conversationType":[NSNumber numberWithInteger:conversationType],
                          @"pageIndex":[NSNumber numberWithInteger:pageIndex],
                          @"pageSize":[NSNumber numberWithInteger:pageSize],
                          @"toUserId":LBStringNotNil(toUserId),@"sort":@"desc"};
    
    [LBService post:LBURL_IM(URL_IM_GetHistoryMessage) params:dic completion:^(LBResponse *response) {
        
        if (response.succeed) {
            
            success(response.data);

            
        }else{
            
            fail(response.message);
            
            
        }
        
        
        
    }];
}

+(void)getIMIdWithUserId:(NSString *)userId success:(LBRCRequestSuccessDataBlock)success fail:(LBRCRequestFailBlock)fail
{
    [LBService get:LBURL_IM(URL_IM_GetChatId) params:@{@"userId":LBStringNotNil(userId)} completion:^(LBResponse *response) {
        
        if (response.succeed) {
            if (response.data) {
                success(response.data);
            }else{
                fail(response.message);
            }

            
        }else{
            
            fail(response.message);
        }

    }];
}

+(void)getBidTenderIMHistoryMessageByContent:(NSString *)content
                                   pageIndex:(NSInteger)pageIndex
                                    toUserId:(NSString *)toUserId
                                     success:(LBRCRequestSuccessDataBlock)success
                                        fail:(LBRCRequestFailBlock)fail
{
    //conversationType: 群组会话是 3
    [self getIMHistoryMessageByContent:content conversationType:3 pageIndex:pageIndex pageSize:10 toUserId:toUserId success:^(NSDictionary *data) {
        success(data);
    } fail:^(NSString *message) {
        fail(message);
    }];
}


/**
 *  获取融云IM Token
 *
 *  @param success 成功回调
 *  @param fail    失败回调
 */
+ (void)getIMTokenWithSuccess:(void(^)(NSString *token))success fail:(void(^)(NSString *message))fail{
    //如果本地已经有Token
    //    if ([LBRCIMManager shareInstance].imInfo.imToken) {
    //        success([LBRCIMManager shareInstance].imInfo.imToken);
    //        return;
    //    }
    [LBService post:LBURL_IM(URL_IM_Token) params:nil completion:^(LBResponse *response) {
        if (response.succeed) {
            [LBRCIMManager shareInstance].rcIMInfo = [LBRCIMInfo mj_objectWithKeyValues:response.data];
            [[LBRCIMManager shareInstance] saveRCIMInfo];
            success([LBRCIMManager shareInstance].rcIMInfo.imToken);
            
        }else{
            fail(response.message);
            
        }
    }];
}

/**
 *  获取融云IM我们服务器本地的Token
 *
 *  @param success 成功回调
 *  @param fail    失败回调
 */
+(void)getIMLocalTokenWithSuccess:(void (^)(NSString *))success fail:(void (^)(NSString *))fail
{
    [LBService post:LBURL_IM(URL_IM_Local_Token) params:nil completion:^(LBResponse *response) {
        if (response.succeed) {
            [LBRCIMManager shareInstance].rcIMInfo = [LBRCIMInfo mj_objectWithKeyValues:response.data];
            
            if (!IsStrEmpty([LBRCIMManager shareInstance].rcIMInfo.imToken)) {
                [[LBRCIMManager shareInstance] saveRCIMInfo];
                success([LBRCIMManager shareInstance].rcIMInfo.imToken);
            }else{
               success(nil);
            }
            
        }else{
            fail(response.message);
            
        }
    }];
}

@end
